const TYPE = {
  SET_TOKEN:"setToken",
  SET_USER:"userInfo",
};

const state = () => ({
  isAuthToken:false,
  userInfo: {}
});

const getters = {
  isAuthToken:state => state.isAuthToken,
  userInfo:state => state.userInfo,
};

const mutations = {
  [TYPE.SET_TOKEN](state,isAuthToken){
    if(isAuthToken){
      state.isAuthToken = isAuthToken;
    }else{
      state.isAuthToken = false;
    }
  },
  [TYPE.SET_USER](state,userInfo){
    if(userInfo){
      state.userInfo = userInfo;
    }else{
      state.userInfo = {};
    }
  },
};

const actions = {
  setIsAuthToken(state,isAuthToken) {
    commit(TYPE.SET_TOKEN,isAuthToken);
  },
  setUserInfo(state,userInfo) {
    commit(TYPE.SET_USER,userInfo);
  },
  setLogin({commit},userInfo){
    return new Promise((resolve,reject) => {
      try {
        commit(TYPE.SET_TOKEN,true);
        commit(TYPE.SET_USER,userInfo);
        resolve(userInfo);
      } catch(error) {
        console.log('%c ! filter router error ','background:#000;color:#bada55',error);
        reject(error);
      }
    });
  }
};

export default {
  state,
  mutations,
  getters,
  actions,
};
